package xyz.yuanjilu.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xyz.yuanjilu.entity.PushEntity;
import xyz.yuanjilu.entity.SalesEntity;
import xyz.yuanjilu.entity.WebpageinfoEntity;
import xyz.yuanjilu.service.GroupInfoService;
import xyz.yuanjilu.service.MailService;
import xyz.yuanjilu.service.PushCentreService;
import xyz.yuanjilu.service.PushService;
import xyz.yuanjilu.service.SalesService;
import xyz.yuanjilu.service.WebSiteService;
import xyz.yuanjilu.service.WebpageinfoService;
import xyz.yuanjilu.utils.DateUtils;

@Service
public class PushCentreServiceImpl implements PushCentreService{
	@Autowired
	private MailService mailService;
	@Autowired
	private PushService pushService;
	@Autowired
	private WebpageinfoService webpageinfoService;
	@Autowired
	private GroupInfoService groupInfoService;
	@Autowired
	private WebSiteService webSiteService;
	@Autowired
	private SalesService salesService;
	@Override
	public void AutoSendWebPageMail() {
		/**
		 * 先查询销售，根据销售获取没有推送过的页面，在吧页面推送出去
		 */
		Map<String, Object> param= new HashMap<String, Object>();
		List<SalesEntity> queryList = salesService.queryList(param);
		for (SalesEntity salesEntity : queryList) {
			param.put("pushType", "email");
			param.put("pushResults", "ok");
			Long salesId = salesEntity.getId();
			param.put("salesId", salesId);
			List<WebpageinfoEntity> queryNoPushWebPageList = webpageinfoService.queryNoPushWebPageList(param);
			if (CollectionUtils.isNotEmpty(queryNoPushWebPageList)) {
				String keywordBysales = groupInfoService.getKeywordBysales(salesId);
				String[] keywordsBysales = groupInfoService.getKeywordsBysales(salesId);
				Set<WebpageinfoEntity> queryNoPushWebPageListtemp = new HashSet<WebpageinfoEntity>();
				for (WebpageinfoEntity webpageinfoEntity : queryNoPushWebPageList) {//根据关键字筛选
					for (String string : keywordsBysales) {
						webpageinfoEntity=webpageinfoService.queryObject(webpageinfoEntity.getId());//根据id获取示例，因为Content在子表中
						String content = webpageinfoEntity.getContent()+"";
						String title = webpageinfoEntity.getTitle()+"";
						String author = webpageinfoEntity.getAuthor()+"";
						if (title.indexOf(string)>-1||content.indexOf(string)>-1||author.indexOf(string)>-1) {
							queryNoPushWebPageListtemp.add(webpageinfoEntity);
						}
					}
				}
				param.clear();
				param.put("webPageList", queryNoPushWebPageListtemp);
				param.put("salesEntity", salesEntity);
				param.put("keywordBysales", keywordBysales);
				mailService.sendMail(salesEntity.getEmail(),"招标信息"+DateUtils.format(new Date(), DateUtils.DATE_PATTERN),"template/email.vm", param);
				for (WebpageinfoEntity webpageinfoEntity : queryNoPushWebPageListtemp) {
					PushEntity push = new PushEntity();
					push.setInfoId(webpageinfoEntity.getId());
					push.setPushDate(new Date());
					push.setPushTarget(salesId.toString());
					push.setPushType("email");
					push.setPushBz(keywordBysales);
					if ("ok".equals(param.get("results"))) {
						//发成功，
						push.setPushResults("ok");
					}else {
						//发送失败
						push.setPushResults("failed");
						String pushBz = param.get("message")+"";
						push.setPushBz(pushBz.substring(0, 900));
					}
					pushService.save(push);
				}
				
			}
			
		}
		
	}

}
